<template>
  <!--弹窗-->
  <el-dialog
    width="35%"
    :title="title"
    :visible.sync="visible"
    :before-close="handleClose">
    <el-form :model="formData" :rules="rules" ref="formData" label-width="100px">
      <el-form-item label="广告图片:" prop="advImg">
        <div>
          <el-upload
            class="avatar-uploader"
            action=""
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :http-request="handlePost">
            <img v-if="formData.cImage" :src="formData.cImage" class="avatar" alt="">
            <i v-else  class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
      </el-form-item>
      <el-form-item label="广告标题:" prop="advTitle">
        <el-input v-model="formData.cTitle" ></el-input>
      </el-form-item>
      <el-form-item label="广告链接:" prop="advUrl">
        <el-input v-model="formData.advertisingLink" ></el-input>
      </el-form-item>
      <el-form-item label="开始时间:">
        <el-date-picker
          v-model="formData.startTime"
          type="datetime"
          placeholder="选择日期时间"
          align="right"
          value-format="yyyy-MM-dd HH:mm:ss"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间:">
        <el-date-picker
          v-model="formData.endTime"
          type="datetime"
          placeholder="选择日期时间"
          align="right"
          value-format="yyyy-MM-dd HH:mm:ss"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <el-row style="margin-top:17px; ">
      <el-button style="float:right;margin-left:6px;" size="mini" type="danger" plain @click="handleClose">取 消</el-button>
      <el-button style="float:right" size="mini" type="primary" @click="submitForm('formData')">确定</el-button>
    </el-row>
  </el-dialog>
</template>
<script>
  /*import * as api from '@/api/homepage'
  import * as apia from '@/api/commonality'*/
  import qs from "qs"
  export default {
    name: 'Edit',
    data() {
      return {
        opts: {
          advTargetList: []
        },
        pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
        rules: {
          cImage: [
            { required: true, message: '请输入广告图片', trigger: 'blur', autocomplete: 'on' }
          ],
          cTitle: [
            { required: true, message: '请输入广告标题', trigger: 'blur' }
          ],
          advertisingLink: [
            { required: true, message: '请输入广告链接', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '请输入开始时间', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '请输入结束时间', trigger: 'blur' }
          ]
        }
      }
    },
    props: {
      // route object
      formData: {
        type: Object,
        default: () => {}
      },
      visible: {
        type: Boolean,
        default: false
      },
      title: {
        type: String,
        default: ''
      }
    },
    mounted() {
      /*this.$store.dispatch('GetOpts', { enumName: "AdvTargetEnum", type: 'arr' }).then(res => {
        this.opts.advTargetList = res
      })*/
    },
    methods: {
      handleChange(value) {
        this.formData.sort = value
      },
      handleClose(done) {
        this.$refs['formData'].resetFields()
        this.$emit('close-callback')
      },
      handleAvatarSuccess(res, file) {
        this.formData.cImage = URL.createObjectURL(file.raw);
      },
      handlePost(file) {
        console.log(file);
        const fd = new FormData();
        fd.append('picFile', file.file)
        let config = {
          headers: {'Content-Type': 'multipart/form-data'}
        };
        this.$axios.post('education-system/sys/carousel/uploadPic', fd, config).then(response => {   //这里的/xapi/upimage为接口
          this.formData.cImage = response.data.obj;
          console.log(this.formData.cImage);
        }).catch(() => {
          this.$message({
            showClose: true,
            message: '上传失败',
            type: 'error'
          });
        })
        // 配置post请求的参数。参数名file,后面跟要传的文件，参数名fileType，值为category（看后端的具体要求）

      },
      submitForm(formName) {
        if (this.formData === undefined) {
          this.$message({
            showClose: true,
            message: '提交失败',
            type: 'error'
          });
        } else {
          if (!this.formData.cImage) {
            this.$message({
              type: 'error',
              message: '请添加广告图片'
            });
            return false
          }
          if (!this.formData.cTitle) {
            this.$message({
              type: 'error',
              message: '请输入广告标题'
            });
            return false
          }
          if (!this.formData.advertisingLink) {
            this.$message({
              type: 'error',
              message: '请输入广告链接'
            });
            return false
          }
          if (!this.formData.startTime) {
            this.$message({
              type: 'error',
              message: '请输入开始时间'
            });
            return false
          }
          if (!this.formData.endTime) {
            this.$message({
              type: 'error',
              message: '请输入结束时间'
            });
            return false
          }
          this.loading=true
          if (this.formData.cid === undefined) {
            this.$axios.post('education-system/sys/carousel/advSave', qs.stringify(this.formData)).then(r=>{
              this.loading=false
              if(r.status == 200 && r.data.obj == true){
                // 提交成功, 关闭窗口, 刷新列表
                this.handleClose('close-callback')
              }else{
                this.$message({
                  showClose: true,
                  message: '提交失败',
                  type: 'error'
                });
              }
            }).catch(() => {
              this.loading=false
            })
            // 新增

          } else {
            // 编辑
            this.loading=true;
            this.$axios.post('education-system/sys/carousel/advSave', qs.stringify(this.formData)).then(r=>{
              this.loading=false
              if(r.status == 200 && r.data.obj == true){
                // 提交成功, 关闭窗口, 刷新列表
                this.handleClose('close-callback')
              }else{
                this.$message({
                  showClose: true,
                  message: '提交失败',
                  type: 'error'
                });
              }
            }).catch(() => {
              this.loading=false
            })

          }
        }
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 150px;
    height: 150px;
    line-height: 150px;
    text-align: center;
  }
  .avatar {
    width: 150px;
    height: 150px;
    display: block;
  }
</style>
